/*
 * Created by JFormDesigner on Tue Oct 19 12:27:15 CST 2021
 */

package view.info;

import library.controller.FrameListener;
import library.po.Book;
import library.po.Borrow;
import library.po.User;
import library.service.BookService;
import library.service.BorrowService;
import library.service.impl.BookServiceImpl;
import library.service.impl.BorrowServiceImpl;
import view.main.MainFrame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author unknown
 */
public class BorrowBooksDialog extends JDialog {
    public BorrowBooksDialog(PersonalInfoDialog owner, User user) {
        super(owner);
        this.setVisible(true);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.user = user;
        this.validate();
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        dialogPane = new JPanel();
        bookService = new BookServiceImpl();
        borrowService = new BorrowServiceImpl();
        borrowList = borrowService.selectByUserId(user.getUserId());
        tableScroll = new JScrollPane();
        buttonBar = new JPanel();
        okButton = new JButton();
        listener = new FrameListener(null,null );
        tablePanel = this.getBookTablePanel(borrowList);
        //======== this ========
        setTitle("\u5df2\u501f\u56fe\u4e66");
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax .
            swing. border .EmptyBorder ( 0, 0 ,0 , 0) ,  "JF\u006frmDesi\u0067ner Ev\u0061luatio\u006e" , javax. swing .border
            . TitledBorder. CENTER ,javax . swing. border .TitledBorder . BOTTOM, new java. awt .Font ( "Dialo\u0067"
            , java .awt . Font. BOLD ,12 ) ,java . awt. Color .red ) ,dialogPane. getBorder
            () ) ); dialogPane. addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java
            . beans. PropertyChangeEvent e) { if( "borde\u0072" .equals ( e. getPropertyName () ) )throw new RuntimeException
            ( ) ;} } );
            dialogPane.setLayout(new BorderLayout());

            //======== tablePanel ========
            {

                tablePanel.setLayout(new GridLayout());

                //======== tableScroll ========
                {
                    //---- bookInfo ----
                    bookInfo.setFont(new Font("Lucida Bright", Font.BOLD, 20));
                    tableScroll.setViewportView(bookInfo);
                }
                tablePanel.add(tableScroll);
            }
            dialogPane.add(tablePanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new FlowLayout());

                //---- okButton ----
                okButton.setText("OK");
                okButton.addActionListener(listener);
                okButton.setActionCommand("InfoExit");
                okButton.setPreferredSize(new Dimension(70, 40));
                buttonBar.add(okButton);
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(825, 505);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }
    private JPanel getBookTablePanel(List<Borrow> borrowList) {
        tablePanel = new JPanel();
        String[] titles = {"书名", "ISBN", "作者", "图书类别", "状态", "借阅时间", "应还时间", "归还时间" };
        Object[][] data = new Object[borrowList.size()][8];
        Iterator<Borrow> iter1 = borrowList.iterator();
        int i = 0;
        while (iter1.hasNext()){
            Borrow borrow = iter1.next();
            Book book = bookService.selectByIsbn(borrow.getBookIsbn());
            data[i][0] = book.getBookName();
            data[i][1] = book.getBookIsbn();
            data[i][2] = book.getBookWriter();
            data[i][3] = book.getBookType();
            data[i][4] = borrow.getBorrowState();
            data[i][5] = borrow.getBorrowTime();
            data[i][6] = borrow.getCorrectReturnTime();
            data[i][7] = borrow.getReturnTime();
            i++;
        }
        DefaultTableModel tableModel = new DefaultTableModel(data,titles);
        bookInfo = new JTable(tableModel);
        bookInfo.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
        bookInfo.repaint();
        tablePanel.add(bookInfo);
        return tablePanel;
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JPanel dialogPane;
    private JPanel tablePanel;
    private JScrollPane tableScroll;
    private JTable bookInfo;
    private JPanel buttonBar;
    private JButton okButton;
    private User user;
    private List<Borrow> borrowList;
    private BookService bookService;
    private BorrowService borrowService;
    private FrameListener listener;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
